package util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class DealParams {
    /**
     * 把request的参数进行处理
     * @return 存入了字段名和值的map
     */
    public static Map<String,String>getParamsMap(HttpServletRequest request) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder streamSB = new StringBuilder();
        while ((line = br.readLine()) != null) {
            streamSB.append(line);
        }
        String decode = URLDecoder.decode(streamSB.toString(), "UTF-8");
        Map<String,String>params = new HashMap<>();
        decode = decode.substring(1,decode.length()-1);
        String resArry[] = decode.split("[:,]");
        for(int i=0;i<resArry.length;i=i+2){
            params.put(resArry[i].substring(1,resArry[i].length()-1),resArry[i+1].substring(1,resArry[i+1].length()-1));
        }
        return params;
    }
}
